﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CheckWindowsServicesUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void InstallService(string serviceName, string programName)
        {
            string ass = System.Environment.CurrentDirectory + "\\" + programName;
            if (!ExistService(serviceName))
            {
                try
                {
                    using (AssemblyInstaller instanller = new AssemblyInstaller(ass, null))
                    {
                        IDictionary state = new Hashtable();
                        instanller.UseNewContext = true;
                        instanller.Install(state);
                        instanller.Commit(state);
                        MessageBox.Show("服务安装成功!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("服务已经存在!");
            }
        }

        private void UninstallService(string serviceName, string programName)
        {
            string ass = System.Environment.CurrentDirectory + "\\" + programName;
            if (ExistService(serviceName))
            {
                try
                {
                    using (AssemblyInstaller instanller = new AssemblyInstaller(ass, null))
                    {
                        IDictionary state = new Hashtable();
                        instanller.UseNewContext = true;
                        instanller.Uninstall(state);
                        MessageBox.Show("服务卸载成功!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("服务不存在!");
            }
        }

        private void StartService(string serviceName, string[] args)
        {
            if (ExistService(serviceName))
            {
                ServiceController service = new ServiceController(serviceName);
                if (service != null && service.Status != ServiceControllerStatus.Running)
                {
                    //string[] args = { "check.conf" };
                    service.Start(args);
                    MessageBox.Show("服务启动成功!");
                }
            }
            else
            {
                MessageBox.Show("服务不存在!");
            }
        }

        private void StopService(string serviceName)
        {
            if (ExistService(serviceName))
            {
                ServiceController service = new ServiceController(serviceName);
                if (service != null && service.Status != ServiceControllerStatus.Stopped)
                {
                    service.Stop();
                    MessageBox.Show("服务停止成功!");
                }
            }
            else
            {
                MessageBox.Show("服务不存在!");
            }
        }

        private bool ExistService(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
                if (sc.ServiceName.Equals(serviceName))
                {
                    return true;
                }
            }
            return false;
        }

        private void btnInstall_Click(object sender, RoutedEventArgs e)
        {
            InstallService("CheckWindowsServices", "CheckWindowsServices.exe");
            //StartService();
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            string[] args = { "check.conf" };
            StartService("CheckWindowsServices", args);
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            StopService("CheckWindowsServices");
        }

        private void btnUninstall_Click(object sender, RoutedEventArgs e)
        {
            UninstallService("CheckWindowsServices", "CheckWindowsServices.exe");
        }
    }
}
